<?php

namespace App\Http\Controllers;

use App\Models\Post;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {

        $user = Auth::user();
        $ids = $user->followings->pluck('id');
        array_pull($ids,$user->id);
        $posts = Post::query()->whereIn('user_id',$ids)->with('user')->orderByDesc('created_at');
        return view('home',compact('posts'));
    }
}
